// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html

/**
 * 我的回水界面
 */
cc.Class({
    extends: cc.Component,

    properties: {
        _selectTg: null, //单选按钮选中(我的回水单选按钮)  
        backWaterPre: cc.Prefab, //回水item预制  
        waterContent: cc.Node,   //内容content
        isNowRefreshing:false,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this._curPage = 1;
        this.isNowRefreshing =false;
        //请求数据
        this.requestRefresh(this._curPage);
    },

    requestRefresh: function(page){
        var recv = function recv(ret) {
            if (ret.Code !== 0) {
                cc.error(ret.Msg);
            }else{
                cc.info('ret数据',ret.Data);
                if(!Utils.isEffective(ret.Data)){
                    ComponentsUtils.unblock();
                    return;
                }
                for(var i=0;i<ret.Data.length;++i){
                    var waterItem = cc.instantiate(this.backWaterPre); 
                    waterItem.getComponent('user_backWater_item').init(ret.Data[i]);
                    this.waterContent.addChild(waterItem);
                }
            }
          
            ComponentsUtils.unblock();
        }.bind(this);

        var data = {
            Token:User.getLoginToken(),
            UserCode:User.getUserCode(),
            PageIndex: page,
            PageSize: 16,
        };
        CL.HTTP.sendRequestRet(DEFINE.HTTP_MESSAGE.POSTREBATERECORD, data, recv.bind(this),"POST");
        ComponentsUtils.block();
       
    },

    init: function(para){
        this._selectTg =para;
    },

    resetRefresh:function(){
        this.waterContent.removeAllChildren();
        this._curPage = 1;
        this.isNowRefreshing = false;
        this.requestRefresh(this._curPage);
    },

    //滚动获取下一页
    scrollCallBack: function (scrollview, eventType) {
        if(eventType === cc.ScrollView.EventType.BOUNCE_BOTTOM)
        {
            //下拉刷新,当底部的offset抵达到某个点的时候
            var offset_y = scrollview.getScrollOffset().y;
            var max_y = scrollview.getMaxScrollOffset().y; 
            if(offset_y - max_y>200){
                if(this.isNowRefreshing == false){
                    this.isNowRefreshing = true;
                    this.requestRefresh(this._curPage);
                } 
            }
        }
    },

    //下拉刷新
    scrollRefresh: function (scrollview, eventType, customEventData) {
        if(eventType === cc.ScrollView.EventType.BOUNCE_TOP)
        {
            //下拉刷新,当底部的offset抵达到某个点的时候
            var offset_y = scrollview.getScrollOffset().y;
            var max_y = scrollview.getMaxScrollOffset().y; 
            if(max_y - offset_y >200){
                this.resetRefresh();
            }
        }
    },


    onClose: function(){
        this._selectTg.isChecked =false;
        this.node.getComponent("Page").backAndRemove();
    },

    // update (dt) {},
});
